package com.dgut.legalservice.service.impl;

import com.dgut.legalservice.dao.IAttachmentDao;
import com.dgut.legalservice.dao.IContractDao;
import com.dgut.legalservice.domain.contract.*;
import com.dgut.legalservice.service.IContractService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("contractService")
public class ContractServiceImpl implements IContractService {

    @Autowired
    private IContractDao contractDao;

    /**
     * 添加合同定制模板
     *
     * @param contract
     */
    @Transactional
    public void addCustomContract(Contract contract) {
        contractDao.insertContract(contract);
        if (contract.getContractSteps() != null) {
            for (ContractStep contractStep : contract.getContractSteps()) {
                contractDao.insertContractStep(contractStep);
                contractDao.insertContractStepRef(contract.getId(), contractStep.getId());
                if (contractStep.getContractItems() != null) {
                    for (ContractItem contractItem : contractStep.getContractItems()) {
                        contractDao.insertContractItem(contractItem);
                        contractDao.insertContractStepItemRef(contractItem.getId(), contractStep.getId());
                    }
                }
            }
        }
        contractDao.insertContractRecord(contract.getId(), contract.getContractRecord());
    }

    /**
     * 添加合同下载模板 无步骤内容
     *
     * @param contract
     */
    @Transactional
    public void addDownloadContract(Contract contract) {
        contractDao.insertContract(contract);
        contractDao.insertContractRecord(contract.getId(), contract.getContractRecord());
    }

    /**
     * 查找所有合同 输出列表 *分页
     *
     * @return
     */
    @Transactional(readOnly = true)
    public List<Contract> findCustomContracts(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return contractDao.findCustomContracts();
    }

    @Transactional(readOnly = true)
    public List<Contract> findDownloadContracts(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return contractDao.findDownloadContracts();
    }

    /**
     * 根据条件动态查询合同列表
     * Script方式 ContractVo类
     *
     * @param vo
     * @return
     */
    @Transactional(readOnly = true)
    public List<Contract> findCustomContractByVo(ContractVo vo, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return contractDao.findCustomContractByVo(vo);
    }

    @Transactional(readOnly = true)
    public List<Contract> findDownloadContractByVo(ContractVo vo, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return contractDao.findDownloadContractByVo(vo);
    }

    /**
     * 查找某个合同 以及步骤内容定制记录
     *
     * @param id
     * @return
     */
    @Transactional(readOnly = true)
    public Contract findContractById(Integer id) {
        return contractDao.findContractById(id);
    }

    /**
     * 更新合同以及步骤内容
     *
     * @param contract
     */
    @Transactional
    public void updateCustomContract(Contract contract) {
        //修改合同
        contractDao.updateContract(contract);
        //修改定制记录
        contractDao.updateContractRecord(contract.getContractRecord());
        //修改步骤内容
        if (contract.getContractSteps() != null) {
            for (ContractStep contractStep : contract.getContractSteps()) {
                contractDao.updateContractStep(contractStep);
                if (contractStep.getContractItems() != null) {
                    for (ContractItem contractItem : contractStep.getContractItems()) {
                        contractDao.updateContractItem(contractItem);
                    }
                }
            }
        }
    }

    @Transactional
    public void updateDownloadContract(Contract contract) {
        //修改合同
        contractDao.updateContract(contract);
        //修改定制记录
        contractDao.updateContractRecord(contract.getContractRecord());
    }

    /**
     * 切换合同状态 禁用（默认）/启用
     *
     * @param contractId
     */
    @Transactional
    public void switchShareStatus(Integer contractId) {
        ContractRecord contractRecord = contractDao.findContractRecord(contractId);
        if (contractRecord.getHasShare().equals("否"))
            contractRecord.setHasShare("是");
        else contractRecord.setHasShare("否");
        contractDao.switchShareStatus(contractRecord);
    }

    /**
     * 删除合同等5个
     *
     * @param contractId
     */
    @Transactional
    public void deleteCustomContract(Integer contractId) {
        Contract contract = contractDao.findContractById(contractId);
        if (contract.getContractSteps() != null) {
            for (ContractStep contractStep : contract.getContractSteps()) {
                if (contractStep.getContractItems() != null) {
                    for (ContractItem contractItem : contractStep.getContractItems()) {
                        contractDao.deleteContractItem(contractItem);
                        contractDao.deleteContractStepItemRef(contractItem, contractStep);
                    }
                }
                contractDao.deleteContractStep(contractStep);
                contractDao.deleteContractStepRef(contract, contractStep);
            }
        }
        contractDao.deleteContract(contract);
        contractDao.deleteContractRecord(contract.getContractRecord());
    }

    @Transactional
    public void deleteDownloadContract(Integer contractId) {
        Contract contract = contractDao.findContractById(contractId);
        contractDao.deleteContract(contract);
        contractDao.deleteContractRecord(contract.getContractRecord());
    }

    //    合同审查
    @Transactional
    public int insertContractAudit(ContractAudit contractAudit) {
        return contractDao.insertContractAudit(contractAudit);
    }

    @Transactional(readOnly = true)
    public ContractAudit findContractAuditById(Integer id) {
        return contractDao.findContractAuditById(id);
    }

    @Transactional
    public int updateContractAudit(ContractAudit contractAudit) {
        return contractDao.updateContractAudit(contractAudit);
    }

    @Transactional(readOnly = true)
    public boolean ifContractAudit(Integer orderId) {
        return contractDao.findContractAuditCountByOrderId(orderId) <= 3;
    }

    @Override
    public ContractAudit findContractAuditByOrderId(Integer orderId) {
        return contractDao.findContractAuditByOrderId(orderId);
    }
}
